
f = {}
ConfigRedisConnectors = {}
redis_connectors = {}
mobile_service_ = nil;
ipvn_service_ = nil;
test  = nil;
creators= {}
CountTry={}

local default_biz = "default_biz"
local preload_dir = "preload"
local biz_dir = "biz"
local local_lfs = lfs

local function master ()
    local biz_name
    local sl
    local util_wait = util.wait_for_next_request
    local getSlave = request.getSlaveID
    local co_resume = coroutine.resume
    local l_f = f
    local l_creator = creators
    local reply_ok = reply.status_type.ok
    local reply_setStatus = reply.setStatus
    while true do
        util.wait_for_next_request()
        -- find slave
        biz_name = getSlave()
        sl = l_f[biz_name]
        if not sl then
            sl = l_f[default_biz]
            biz_name = default_biz
        end
        local value, errrorMessage = co_resume(sl)
        if not value then
            reply_setStatus(reply_ok)
            io.log("e", biz_name, errrorMessage)
            l_creator[biz_name]()
        end
    end
end

local function load_dir(dot)
    for name in local_lfs.dir(dot) do
        if string.byte(name,1) ~= 46 then
            local fqn = dot.."/"..name
            local attr = local_lfs.attributes(fqn)
            if (attr.mode == "file") then
                dofile(fqn)
            end
        end
    end
end

-- F=Fatal, E=Error, D=Debug
load_dir(preload_dir)
load_dir(biz_dir)
for l,v in pairs(creators) do
    --print("Create", l)
    v()
end

logs = {}
logs["f"]=io.open("log/fatal.log","w+")
logs["f"]:setvbuf("line")
local min_log_level = config.log_level
if min_log_level=="e" then
    logs["e"]=io.open("log/error.log","a+")
    logs["e"]:setvbuf("line")
elseif min_log_level=="d" then
    logs["e"]=io.open("log/error.log","a+")
    logs["e"]:setvbuf("line")
    logs["d"]= io.open("log/debug.log","a+")
end

io.log = function (level, group, msg)
    if logs[level] then
        logs[level]:write(os.date("%d/%m/%Y %H:%M:%S",os.time()),"\t",group, "\t",msg,"\n")
    end
end
master()


